package ua.soso.dao.hibernate;

import org.hibernate.Query;
import ua.soso.dao.FileDao;
import ua.soso.dao.model.File;
import ua.soso.dao.model.FileCategory;

import java.util.List;

/**
 * Created by Babka Aleksey
 * Date: 19.04.12
 */
public class HibernateFileDao extends HibernateBaseDao<File> implements FileDao {
    public HibernateFileDao() {
        super(File.class);
    }

    @Override
    public void save(File file) {
        session().save(file);
    }

    @Override
    @SuppressWarnings(value = "unchecked")
    public List<File> findAll(long userId) {
        Query query = session().createQuery("from File f where" +
                " f.published = true" +
                " or f.user.id = :USER_ID");

        query.setParameter("USER_ID", userId);

        return query.list();
    }

    @Override
    public File findById(long id) {
        return (File) session().get(File.class, id);
    }

    @Override
    @SuppressWarnings(value = "unchecked")
    public List<File> findByUserId(long userId) {
        Query query = session().createQuery("from File f where" +
                " f.user.id = :USER_ID");

        query.setParameter("USER_ID", userId);

        return query.list();
    }

    @Override
    @SuppressWarnings(value = "unchecked")
    public List<FileCategory> findCategories() {
        return session().createCriteria(FileCategory.class).list();
    }
}
